home *** CD-ROM | disk | FTP | other *** search
/ Celestin Apprentice 2 / Apprentice-Release2.iso / Source Code / C / Libraries / stdwin / Ports / alfa / syswin.c < prev    next >
Encoding:
C/C++ Source or Header  |  1991-04-16  |  6.6 KB  |  364 lines  |  [TEXT/????]

  1. /* STDWIN -- SYSTEM WINDOW. */
  2.  
  3. #include "alfa.h"
  4.  
  5. WINDOW *syswin;        /* Window id 0, the system window */
  6.             /* Global because wgetevent needs to know about
  7.                it, so it can suppress events belonging to
  8.                this window. */
  9.  
  10. static void
  11. helpmessage()
  12. {
  13.     char buf[256];
  14.     char shortcut[256];
  15.     
  16.     getbindings(shortcut, 0, MENU_CALL);
  17.     sprintf(buf, "[Use  %s  to get a menu of commands]", shortcut);
  18.     wmessage(buf);
  19. }
  20.  
  21. void
  22. initsyswin()
  23. {
  24.     syswin= wopen("System", wsysdraw);
  25.     helpmessage();
  26. }
  27.  
  28. char *sysmsg;        /* Message to be drawn at (0, 0) */
  29. TEXTEDIT *syste;    /* Textedit record to be drawn, too */
  30.  
  31. #ifdef EM
  32. static char **Butt=NULL;
  33. #endif
  34.  
  35. /*ARGSUSED*/
  36. void
  37. wsysdraw(win, left, top, right, bottom)
  38.     WINDOW *win;
  39.     int left, top;
  40.     int right, bottom;
  41. {
  42.     if (sysmsg != NULL) {
  43.         (void) wdrawtext(0, 0, sysmsg, -1);
  44.         if (syste != NULL)
  45.             tedraw(syste);
  46. #ifdef EM 
  47.         if(Butt)
  48.             drawbuttons();    
  49. #endif
  50.     }
  51.     else
  52.         drawmenubar();
  53. }
  54.  
  55. void
  56. menubarchanged()
  57. {
  58.     uptodate[0]= FALSE;
  59. }
  60.  
  61. /* Print a message in the system window.
  62.    If the message is non-null, the screen is updated immediately. */
  63.  
  64. void
  65. wmessage(str)
  66.     char *str;
  67. {
  68.     if (sysmsg != NULL)
  69.         free(sysmsg);
  70.     sysmsg= strdup(str);
  71.     if (syste != NULL) {
  72.         tefree(syste);
  73.         syste= NULL;
  74.     }
  75.     wchange(syswin, 0, 0, 9999, 9999);
  76.     wnocaret(syswin);
  77.     if (str != NULL) {
  78.         wupdate(syswin);
  79.         wflush();
  80.     }
  81. }
  82.  
  83. /* Ask for an input string. */
  84.  
  85. bool
  86. waskstr(prompt, buf, len)
  87.     char *prompt;
  88.     char *buf;
  89.     int len;
  90. {
  91.     WINDOW *savewin= front;
  92.     WINDOW *win= syswin;
  93.     bool ok= FALSE;
  94.     bool stop= FALSE;
  95.     int teleft;
  96.     
  97.     wsetactive(win);
  98.     wmessage((char *) NULL);
  99.     sysmsg= prompt;
  100.     teleft= wtextwidth(prompt, -1) + wtextwidth(" ", 1);
  101.     if (teleft > columns * 3/4)
  102.         teleft= columns * 3/4;
  103.     syste= tealloc(syswin, teleft, 0, columns-teleft);
  104.     tereplace(syste, buf);
  105.     tesetfocus(syste, 0, 9999);
  106.     do {
  107.         EVENT e;
  108.         
  109.         if (!wsysevent(&e, FALSE)) {
  110.             wupdate(syswin);
  111.             wflush();
  112.             wsysevent(&e, TRUE);
  113.         }
  114.         e.window= syswin; /* Not filled in by wsys*event();
  115.                      needed by teevent(). */
  116.             
  117.         switch (e.type) {
  118.         
  119.         case WE_MENU:
  120.             if (e.u.m.id != 0) {
  121.                 wfleep();
  122.                 break;
  123.             }
  124.             switch (e.u.m.item) {
  125.             
  126.             case SUSPEND_PROC:
  127.                 _wsuspend();
  128.                 break;
  129.             
  130.             case REDRAW_SCREEN:
  131.                 _wredraw();
  132.                 break;
  133.             
  134.             default:
  135.                 if (e.u.m.item >= FIRST_CMD &&
  136.                     e.u.m.item <= LAST_CMD)
  137.                     wsyscommand(&e);
  138.                 break;
  139.             }
  140.             if (e.type != WE_COMMAND)
  141.                 break;
  142.         
  143.         /* Fall through from previous case! */
  144.         case WE_COMMAND:
  145.             switch (e.u.command) {
  146.             
  147.             case WC_RETURN:
  148.             case WC_CANCEL:
  149.                 ok= e.u.command == WC_RETURN;
  150.                 stop= TRUE;
  151.                 break;
  152.             
  153.             default:
  154.                 if (!teevent(syste, &e))
  155.                     wfleep();
  156.                 break;
  157.             
  158.             }
  159.             break;
  160.         
  161.         case WE_CHAR:
  162.         case WE_MOUSE_DOWN:
  163.         case WE_MOUSE_MOVE:
  164.         case WE_MOUSE_UP:
  165.             if (!teevent(syste, &e))
  166.                 wfleep();
  167.             break;
  168.         
  169.         }
  170.     } while (!stop);
  171.     if (ok) {
  172.         strncpy(buf, tegettext(syste), len);
  173.         buf[len-1]= EOS;
  174.     }
  175.     sysmsg= NULL;
  176.     wmessage((char *) NULL);
  177.     wsetactive(savewin);
  178.     return ok;
  179. }
  180.  
  181. #ifdef EM
  182. /* EuroMath hacks -- I still hop I can get rid of this again... */
  183. #define META(c) ((c)|128)
  184. #define UNMETA(c) ((c)&~128)
  185. #define min(a,b) (a)<(b)?(a):(b)
  186. char *hack;
  187. int Curr;
  188.  
  189. wdialog(prompt, buf, len, butt, def)
  190. char *prompt, *buf, **butt;
  191. int len, def;
  192. {
  193.     WINDOW *savewin= front;
  194.     WINDOW *win= syswin;
  195.     bool ok= FALSE;
  196.     bool stop= FALSE;
  197.     int teleft;
  198.     int y=1, i;    
  199.     extern char* esc;
  200.     extern char *expandCommand();
  201.  
  202.     if(butt==NULL)
  203.         return waskstr(prompt, buf, len);
  204.  
  205.     Butt=butt;
  206.     Curr=0;
  207. /* highlight the default button */
  208.     for(i=0;butt[def-1][i];i++) butt[def-1][i]=META(butt[def-1][i]);
  209.     wsetactive(win);
  210.     wmessage((char *) NULL);
  211.     sysmsg= prompt;
  212.     teleft= wtextwidth(prompt, -1) + wtextwidth(" ", 1);
  213.     if (teleft > columns * 3/4)
  214.         teleft= columns * 3/4;
  215.     syste= tealloc(syswin, teleft, 0, columns-teleft);
  216.     tereplace(syste, strdup(buf));
  217.     tesetfocus(syste, 0, 9999);
  218. /* calculate the number of buttons */
  219.     while(butt[y-1]) {
  220.         ++y;
  221.     }
  222.     --y;
  223. restart:
  224.     do {
  225.         EVENT e;
  226.         
  227.         if (!wsysevent(&e, FALSE)) {
  228.             wupdate(syswin);
  229.             wflush();
  230.             wsysevent(&e, TRUE);
  231.         }
  232.         e.window= syswin; /* Not filled in by wsys*event();
  233.                      needed by teevent(). */
  234.             
  235.         switch (e.type) {
  236.         case WE_MENU:    
  237.             if (e.u.m.id != 0) {
  238.                 wfleep();
  239.                 break;
  240.             }
  241.             switch (e.u.m.item) {
  242.             
  243.             case SUSPEND_PROC:
  244.                 _wsuspend();
  245.                 break;
  246.             
  247.             case REDRAW_SCREEN:
  248.                 _wredraw();
  249.                 break;
  250.             
  251.             default:
  252.                 if (e.u.m.item >= FIRST_CMD &&
  253.                     e.u.m.item <= LAST_CMD)
  254.                     wsyscommand(&e);
  255.                 break;
  256.             }
  257.             if (e.type != WE_COMMAND)
  258.                 break;
  259.         
  260.         /* Fall through from previous case! */
  261.         case WE_COMMAND:
  262.             switch (e.u.command) {
  263. /* the arrow-keys (up & down) are used to escape from the texedit
  264.     buffer and select a button from the buttonlist */
  265.             case WC_UP:
  266.                 --Curr;
  267.                 if(Curr<0)
  268.                     Curr=0;
  269.                 break;
  270.             case WC_DOWN:
  271.                 ++Curr;
  272.                 if(Curr>y)
  273.                     Curr=0;    
  274.                 break;
  275.             case WC_RETURN:
  276.             case WC_CANCEL:
  277.                 ok= e.u.command == WC_RETURN;
  278.                 stop= TRUE;
  279.                 break;
  280.             
  281.             default:
  282.                 if (Curr > 0)
  283.                     wfleep();
  284.                 else if (!teevent(syste, &e))
  285.                     wfleep();
  286.                 break;
  287.             
  288.             }
  289.             break;
  290.         
  291.         case WE_CHAR:
  292.             if(hack && e.u.character == *esc) {
  293. /* this is used by cmdbox(): pressing the `escape' character causes
  294. the typed in buffer to be expanded */
  295.                 char *buf2;
  296.                 tesetfocus(syste,0,9999);
  297.                 strzcpy(buf,tegettext(syste),min(len,tegetlen(syste)+1));
  298.                 buf2=expandCommand(hack, buf);
  299.                 tereplace(syste, buf2);
  300.                 tesetfocus(syste,0,9999);
  301.                 break;
  302.             }
  303.         case WE_MOUSE_DOWN:
  304.         case WE_MOUSE_MOVE:
  305.         case WE_MOUSE_UP:
  306.             if(Curr > 0)
  307.                 wfleep();
  308.             else if (!teevent(syste, &e))
  309.                 wfleep();
  310.             break;
  311.         
  312.         }
  313.     } while (!stop);
  314.     if(hack && Curr == 3) {
  315. /* button 3 is "EXPAND".  Used by cmdbox() */
  316.         char *buf2;
  317.         tesetfocus(syste,0,9999);
  318.         strzcpy(buf,tegettext(syste),min(len,tegetlen(syste)+1));
  319.         buf2=expandCommand(hack, tegettext(syste));
  320.         tereplace(syste, buf2);
  321.         tesetfocus(syste,0,9999);
  322.         ok=stop=FALSE; Curr=0;
  323.         goto restart;
  324.     }    
  325.     if (ok) {
  326.         strzcpy(buf, tegettext(syste), min(len, tegetlen(syste)+1));
  327.     }
  328.     sysmsg= NULL;
  329.     wmessage((char *) NULL);
  330.     wsetactive(savewin);
  331.     if(Curr==0) Curr=def;
  332.     Butt=NULL;
  333.     for(i=0;butt[def-1][i];i++) butt[def-1][i]=UNMETA(butt[def-1][i]);
  334.     _wredraw();
  335.     return Curr;
  336. }
  337.  
  338. /* this is inserted in the syswin drawproc whenever there are buttons
  339. active */
  340.  
  341. drawbuttons() 
  342. {
  343. int y=1;
  344. char buf[256];
  345.  
  346.     while(Butt[y-1]) {
  347.         if(y==Curr) sprintf(buf, "%c %s", META('o'), Butt[y-1]); 
  348.         else sprintf(buf, "o %s", Butt[y-1]);
  349.         trmputdata(y, y, 0, buf, (char *)0);
  350.         ++y;
  351.     }
  352.     trmputdata(y,y,0,"",(char *)0);
  353. }
  354.  
  355. /* additional EuroMath WM routine, called after returning from an
  356. Editor-session (OpenTTY) */
  357.  
  358. RedrawAll()
  359. {
  360. _wredraw();
  361. }
  362.  
  363. #endif
  364.